package es.gw2eventtracker.api;

import java.io.IOException;
import java.util.List;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.client.ClientConfig;

import com.example.gw2eventtracker.rest.JsonMediaRequestFilter;

import es.gw2eventtracker.modelo.Event;
import es.gw2eventtracker.modelo.EventDetails;
import es.gw2eventtracker.modelo.Map;
import es.gw2eventtracker.modelo.World;
import es.gw2eventtracker.modelo.WorldEvents;

public class GW2API {
	public static String apiBaseUrl = "https://api.guildwars2.com/v1/";

	static WebTarget api;
	static MediaType json = MediaType.APPLICATION_JSON_TYPE;
	static {
		api = ClientBuilder.newClient(
				new ClientConfig().register(JsonMediaRequestFilter.class))
				.target(apiBaseUrl);
	}

	public static List<World> getMundos(String idioma) {
		return api.path("world_names.json").queryParam("lang", idioma)
				.request().get(new GenericType<List<World>>() {
				});
	}

	public static List<Event> getNombresEventos(String idioma) {
		return api.path("event_names.json").queryParam("lang", idioma)
				.request().get(new GenericType<List<Event>>() {
				});
	}

	public static List<Event> getEventosMundo(Integer idMundo, Integer idMapa,
			Integer idEvento) {
		return api.path("events.json").queryParam("world_id", idMundo)
				.queryParam("map_id", idMapa).queryParam("event_id", idEvento)
				.request().get(WorldEvents.class).getEvents();
	}

	public static List<Map> getMapas(String idioma) {
		return api.path("map_names.json").queryParam("lang", idioma).request()
				.get(new GenericType<List<Map>>() {
				});
	}

	public static java.util.Map<String, Event> getDetallesEvento(
			String idEvento, String idioma) {
		String string = api.path("event_details.json")
				.queryParam("event_id", "021D4696-82A5-496D-8C53-337CFABED00F")
				.queryParam("lang", idioma).request().get(String.class);

		Object o = api.path("event_details.json")
				.queryParam("event_id", "021D4696-82A5-496D-8C53-337CFABED00F")
				.queryParam("lang", idioma).request().get(EventDetails.class);
		return null;
	}

	public static void main(String[] args) throws IOException {
		// List<World> mundos = GW2API.getMundos("ES");
		// // System.out.println(mundos);
		// System.out.println(mundos.get(0).getName());
		// System.out.println(mundos.size());
		// System.out.println("Fin1!");
		// List<Event> nombresEventos = GW2API.getNombresEventos("ES");
		// // System.out.println(nombresEventos);
		// System.out.println(nombresEventos.get(0).getName());
		// System.out.println(nombresEventos.size());
		// System.out.println("Fin2!");
		//
		// List<Event> eventosMundo1 = GW2API.getEventosMundo(mundos.get(0)
		// .getId(), null, null);
		// // System.out.println(eventosMundo1);
		// System.out.println(eventosMundo1.get(0).getState());
		// System.out.println(eventosMundo1.size());
		// System.out.println("Fin3!");
		//
		// List<Map> mapas = GW2API.getMapas("ES");
		// // System.out.println(mapas);
		// System.out.println(mapas.size());
		// System.out.println(mapas.get(0).getName());

		java.util.Map<String, Event> detallesEvento = GW2API.getDetallesEvento(
				"021D4696-82A5-496D-8C53-337CFABED00F", "ES");
		System.out.println(detallesEvento);
		System.out.println(detallesEvento.size());
	}
}
